﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ImageTools;
using System.IO;
using ImageTools.IO.Jpeg;
using AssessmentTool.Controls;
using AssessmentTool.ServiceBridge;
using AssessmentTool.Services.DTO;
using AssessmentTool.PresentationLogic.EventArguments;
using AssessmentTool.Services;
using ModelLayer;
using System.ComponentModel;


namespace AssessmentTool.PresentationLogic.PDF
{
    public class PDFGenerator
    {
        
        public void generateRadarPDF(Radar radar, ReportRadarInfoDTO reportRadarSelected)
        {

            ExtendedImage img = radar.ToImage();
            JpegEncoder encoder = new JpegEncoder();
            MemoryStream ms = new MemoryStream();
            encoder.Encode(img, ms);

            

            EventDispatcher.getInstance().dispatch("BUSY", null);
            ReportServiceBridge.getPdfProfilo(downloadPdfRadar, ms.ToArray(), reportRadarSelected);
            ms.Close();
            	
        }
        public void generateCompetenzePDF()
        {

            EventDispatcher.getInstance().dispatch("BUSY", null);
            ReportServiceBridge.getPdfCompetenze(downloadPdfCompetenze, (Utente)Session.getInstance().getValue("USER"));
            

        }
        public void downloadPdfRadar(object sender, createPDFRadarCompletedEventArgs args)
        {
            downloadPdf(new FileDownloadedEventArgs() { file = args.Result }, args.Error);
        }


        public void downloadPdfCompetenze(object sender, createPDFCompetenzeCompletedEventArgs args)
        {
            downloadPdf(new FileDownloadedEventArgs() { file = args.Result },args.Error);


        }

        public void downloadPdf(FileDownloadedEventArgs args, Exception error)
        {
            EventDispatcher.getInstance().dispatch("STOPBUSY", null);
            if (error == null)
            {
                ModalWindow ew = new ModalWindow();
                ew.Show("Download del pdf completato!", "Vuoi salvare il pdf?", ModalType.YESNO, "SAVELOCALPDF", args);
            }
            else
            {
                EventDispatcher.getInstance().dispatch("SHOWMESSAGE", new ShowMessageEventArgs()
                {
                    Testo = "Attenzione! Errore critico nella creazione del pdf!",
                    Error = true,
                    Critico = false
                });
            }


        }
     
    }
}
